﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EditViewDialog.cs" company="YellowFish">
//   Copyright © YellowFish 2009
// </copyright>
// <summary>
//   Describes and manipulates the edit view dialog.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MobilePal.Dialogs
{
  #region Imported Namespaces


  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Windows.Forms;


  #endregion


  /// <summary>
  /// Describes and manipulates the edit view dialog.
  /// </summary>
  public partial class EditViewDialog: Form
  {
    #region Constructors and Destructors


    /// <summary>
    /// Initializes a new instance of the <see cref="EditViewDialog"/> class.
    /// </summary>
    public EditViewDialog()
    {
      InitializeComponent();

      List<ApplicationClassInfo> classInfos = Program.GetApplicationViewClassInfos();
      typeComboBox.DisplayMember = "DisplayName";

      foreach (ApplicationClassInfo classInfo in classInfos)
      {
        typeComboBox.Items.Add(classInfo);
      }

      UpdateControls();
    }


    #endregion


    #region Properties


    /// <summary>
    /// Gets the view class.
    /// </summary>
    /// <value>
    /// The view class.
    /// </value>
    public Type ViewClass
    {
      get
      {
        return typeComboBox.SelectedItem != null ? ((ApplicationClassInfo)typeComboBox.SelectedItem).Type : null;
      }
    }


    /// <summary>
    /// Gets the name of the view.
    /// </summary>
    /// <value>
    /// The name of the view.
    /// </value>
    public string ViewName
    {
      get
      {
        return nameTextBox.Text;
      }
    }


    #endregion


    #region Methods


    private void CancelMenuItemClick(object sender, EventArgs e)
    {
      DialogResult = DialogResult.Cancel;

      Close();
    }


    private void NameTextBoxTextChanged(object sender, EventArgs e)
    {
      UpdateControls();
    }


    private void OkMenuItemClick(object sender, EventArgs e)
    {
      DialogResult = DialogResult.OK;

      Close();
    }


    private void TypeComboBoxSelectedIndexChanged(object sender, EventArgs e)
    {
      UpdateControls();
    }


    private void UpdateControls()
    {
      okMenuItem.Enabled = typeComboBox.SelectedIndex >= 0 && !string.IsNullOrEmpty(nameTextBox.Text);
    }


    /// <summary>
    /// Raises the <see cref="E:System.Windows.Forms.Form.Closing"/> event.
    /// </summary>
    /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs"/> that contains the event data.</param>
    protected override void OnClosing(CancelEventArgs e)
    {
      if(DialogResult == DialogResult.Cancel)
      {
        return;
      }

      if (string.IsNullOrEmpty(nameTextBox.Text))
      {
        MessageBox.Show("Invalid view name.", "Invalid Name", MessageBoxButtons.OK, MessageBoxIcon.Hand,
                        MessageBoxDefaultButton.Button1);

        e.Cancel = true;
      }

      if (typeComboBox.SelectedItem == null)
      {
        MessageBox.Show("Please select view type.", "Invalid Type", MessageBoxButtons.OK, MessageBoxIcon.Hand,
                        MessageBoxDefaultButton.Button1);

        e.Cancel = true;
      }
    }


    #endregion
  }
}